import os
import re
import sys


_cfg_add_layer_index_regexp = re.compile('^\s*\[[^\[\]]+\]\s*$')


def cfg_add_layer_index(cfg_path):
    with open(cfg_path, 'r') as f:
        xlines = f.readlines()
        pos = -1
        index = 0
        first = True
        xlines_res = xlines.copy()
        for xline in xlines:
            pos += 1
            if _cfg_add_layer_index_regexp.match(xline):
                if first:
                    first = False
                    continue
                else:
                    xlines_res.insert(pos, f'###{index}\n')
                    index += 1
                    pos += 1
        return xlines_res


def _main():
    path = r'D:\_const\svn\aliyun\cmpltrtok\content\c_darknet\darknet-debug-cpu\cfg\csdarknet53.cfg'
    argc = len(sys.argv)
    if argc >= 2:
        path = sys.argv[1]

    xlist = cfg_add_layer_index(path)
    for xline in xlist:
        print(xline, end='')

if '__main__' == __name__:
    _main()